
const axios = require('axios')
const semver = require('semver')
const chalk = require('chalk')
const package = require('../package.json')
const ora = require('ora')

async function checkVersion () {
  const spinner = ora('版本检测中请稍后...')
  try {
    spinner.start()
    const result = await axios.get("https://registry.npmjs.org/auto-vue-generator")
    if (result.status === 200 && result.data) {
      const latestVersion = result.data['dist-tags'].latest
      const localVersion = package.version
      if (semver.lt(localVersion, latestVersion)) {
        spinner.text = '';
        spinner.succeed()
        console.log()
        console.log(chalk.yellow('  ★ 发现新版本 ★'))
        console.log()
        console.log('  latest:    ' + chalk.green(latestVersion))
        console.log('  installed: ' + chalk.red(localVersion))
        console.log()
        console.log(chalk.yellow('  Run 请执行 `npm i auto-vue-generator@latest -g` 更新'))
      } else {
        spinner.text = '未发现新版本';
        spinner.succeed()
      }
    }
  } catch (error) {
    spinner.text = '未发现新版本';
    spinner.succeed()

  }

}

module.exports = checkVersion()